package com.iflytek.uoamp.agent.monitor.find;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.iflytek.uoamp.agent.common.util.ReadFileByLine;
import com.iflytek.uoamp.agent.common.util.WFile;
import com.iflytek.uoamp.agent.core.sender.monitorsender.responseObj.Program;
import com.iflytek.uoamp.agent.monitor.mcagent.MCAgentConfig;

public class Filter {
	
	private String path;
	private Map<String,Long> set = new HashMap<String,Long>(); 
	public Filter(String path){
		this.path = path;
	}
	
	// key is ip+cpname+port
	// value is find time
	public void init(){
		List<String> line = ReadFileByLine.getAllLine2Array(path, "utf-8", 0, -1);
		for(String cur:line){
			String[] kvs = cur.split(",");
			if(kvs.length==2){
				set.put(kvs[0],Long.parseLong(kvs[1]));
			}else{
				set.put(kvs[0],System.currentTimeMillis());
			}
			
		}
	}
	

	/**
	 * add to cache file,and flush memory set to file
	 * @param program
	 */
	public void add(Program program){
		if(program.getFlags()==null || program.getFlags().size()==0){
			program.setFlags(new LinkedList<String>());
			program.getFlags().add("");
		}
		set.put(program.getIp()+"|||"+program.getCpname()+"|||"+program.getPort()+"|||"+program.getFlags().get(0),System.currentTimeMillis());
		
		WFile.wf(path, "","utf-8",false);
		
		Map<String,Long> tempSet = new HashMap<String,Long>(set);
		Iterator<Entry<String, Long>> it = tempSet.entrySet().iterator();
		while(it.hasNext()){
			Entry<String, Long> entry = it.next();
			WFile.wf(path, entry.getKey()+","+entry.getValue()+"\n","utf-8",true);
		}

	}
	
	
	/**
	 * 是否添加组件;每次调用改方法都会更新缓存时间
	 * @param program
	 * @return
	 */
	public boolean isAdd(Program program){
		if(program.getFlags()==null || program.getFlags().size()==0){
			program.setFlags(new LinkedList<String>());
			program.getFlags().add("");
		}
		boolean isadd =  !set.containsKey(program.getIp()+"|||"+program.getCpname()+"|||"+program.getPort()+"|||"+program.getFlags().get(0));
		if(!isadd){
			set.put(program.getIp()+"|||"+program.getCpname()+"|||"+program.getPort()+"|||"+program.getFlags().get(0),System.currentTimeMillis());
			
			WFile.wf(path, "","utf-8",false);
			Map<String,Long> tempSet = new HashMap<String,Long>(set);
			Iterator<Entry<String, Long>> it = tempSet.entrySet().iterator();
			while(it.hasNext()){
				Entry<String, Long> entry = it.next();
				WFile.wf(path, entry.getKey()+","+entry.getValue()+"\n","utf-8",true);
			}
		}
		return isadd;
	}
	
	/**
	 * this function will calculation long time no find items and add them to need send delete list
	 * @return
	 */
	public List<String> queryNeedDeleItem(){
		Map<String,Long> tempSet = new HashMap<String,Long>(set);
		List<String> list = new LinkedList<String>();
		Iterator<Entry<String, Long>> it = tempSet.entrySet().iterator();
		while(it.hasNext()){
			Entry<String, Long> entry = it.next();
			long temp = entry.getValue();
			long notactivetime = System.currentTimeMillis()-temp;
			if(notactivetime==0){
				continue;
			}
			notactivetime = notactivetime/(1000*60);
			if(notactivetime>MCAgentConfig.getInstance().getNot_active_time()){
				//send del message;
				list.add(entry.getKey());
			}
		}
		return list;
	}
	
	/**
	 * remove success ,flush memory and file;
	 * @param keys
	 */
	public void remove(List<String> keys){
		for(String key:keys){
			set.remove(key);
		}
		WFile.wf(path, "","utf-8",false);
		Map<String,Long> tempSet = new HashMap<String,Long>(set);
		Iterator<Entry<String, Long>> it = tempSet.entrySet().iterator();
		while(it.hasNext()){
			Entry<String, Long> entry = it.next();
			WFile.wf(path, entry.getKey()+","+entry.getValue()+"\n","utf-8",true);
		}
	}
	
}
